package sg.olaparty.network

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import sg.olaparty.network.base.NetworkConfigProvider
import java.util.concurrent.TimeUnit

/**
 * Time:3/15/21 7:54 PM
 * Author:
 * Description:
 */
object RetrofitManager {
    private val okHttpClient by lazy {
        OkHttpClient.Builder().apply {
            connectTimeout(10, TimeUnit.SECONDS)
            writeTimeout(10, TimeUnit.SECONDS)
            readTimeout(20, TimeUnit.SECONDS)
            addInterceptor(HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            })

            hostnameVerifier { _, _ -> true }
        }.build()
    }

    private val retrofit: Retrofit = Retrofit.Builder()
        .callFactory(okHttpClient)
        .baseUrl(NetworkConfigProvider.API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    fun <T> of(tClass: Class<T>): T {
        return retrofit.create(tClass)
    }
}